unit CipherTools;

interface

uses
  Cipher, DECUtil;

const
  ALGORITMO_CIFRADO_DEFAULT = 'Blowfish';
  ALGORITMO_MD5             = 'Message Digest 5';
  CLAVE_CIFRADO_DEFAULT     = 'M9A8E7B6M5F4';

function EncodeStr(const aCadena: string; aAlgoritmo: string = ALGORITMO_CIFRADO_DEFAULT; aClave:string = CLAVE_CIFRADO_DEFAULT;
                   aModoCifrado:TCipherMode=cmCTS;aStringTransform:integer=fmtDEFAULT;
                   aAlgoritmoHash: string='';aIV: Pointer=nil): string;
function DecodeStr(const aCadena: string; aAlgoritmo: string = ALGORITMO_CIFRADO_DEFAULT; aClave:string = CLAVE_CIFRADO_DEFAULT;
                   aModoCifrado:TCipherMode=cmCTS;aStringTransform:integer=fmtDEFAULT;
                   aAlgoritmoHash: string='';aIV: Pointer=nil): string;
function StrToMD5(const aCadena:string):string;

implementation

uses
  HCMngr, Cipher1;

function EncodeStr(const aCadena: string; aAlgoritmo: string;aClave:string;
                   aModoCifrado:TCipherMode;aStringTransform:integer;
                   aAlgoritmoHash: string;aIV:Pointer): string;
var
  CipherManager: TCipherManager;
  HashManager: THashManager;
begin
  Result := '';
  HashManager := nil;
  CipherManager := nil;
  try
    HashManager := THashManager.Create(nil);
    CipherManager := TCipherManager.Create(nil);
    if aAlgoritmoHash <> '' then
    begin
      HashManager.Algorithm := aAlgoritmoHash;
      CipherManager.HashManager := HashManager;
    end;
    CipherManager.Algorithm := aAlgoritmo;
    CipherManager.Mode := aModoCifrado;
    CipherManager.InitKey(aClave, aIV);
    Result :=  CipherManager.Cipher.CodeString(aCadena, paEncode, aStringTransform);
  finally
    CipherManager.Free;
    HashManager.Free;
  end;
end;

function DecodeStr(const aCadena: string; aAlgoritmo: string;aClave:string;
                   aModoCifrado:TCipherMode;aStringTransform:integer;
                   aAlgoritmoHash: string;aIV: Pointer): string;
var
  CipherManager: TCipherManager;
  HashManager: THashManager;
begin
  Result := '';
  HashManager := nil;
  CipherManager := nil;
  try
    HashManager := THashManager.Create(nil);
    CipherManager := TCipherManager.Create(nil);
    if aAlgoritmoHash <> '' then
    begin
      HashManager.Algorithm := aAlgoritmoHash;
      CipherManager.HashManager := HashManager;
    end;
    CipherManager.Algorithm := aAlgoritmo;
    CipherManager.Mode := aModoCifrado;
    CipherManager.InitKey(aClave, aIV);
    Result :=  CipherManager.Cipher.CodeString(aCadena, paDecode, aStringTransform);
  finally
    CipherManager.Free;
    HashManager.Free;
  end;
end;

function StrToMD5(const aCadena:string):string;
var
  hm : THashManager;
begin
  Result := '';
  hm := THashManager.Create(nil);
  try
    Assert(hm <> nil);
    hm.Algorithm := ALGORITMO_MD5;
    hm.CalcString(aCadena);
    Result := hm.DigestString[fmtHEX];
  finally
    hm.Free;
  end;
end;

end.

